#include <iostream>
#include <pthread.h>
#include <unistd.h>

__thread int cnt = 0;

// 线程1，修改并读cnt
void *routine1(void *)
{
    while (true)
    {
        std::cout << "thread-1," << "cnt->" << cnt++ << std::endl;
        sleep(1);
    }
    return nullptr;
}

// 线程2，只读cnt
void *routine2(void *)
{
    while (true)
    {
        std::cout << "thread-2," << "cnt->" << cnt << std::endl;
        sleep(1);
    }
    return nullptr;
}

int main()
{
    pthread_t tid1, tid2;
    pthread_create(&tid1, nullptr, routine1, nullptr);
    pthread_create(&tid2, nullptr, routine2, nullptr);

    pthread_join(tid1, nullptr);
    pthread_join(tid2, nullptr);

    return 0;
}